package com.djk.daily.thread.stopthreads;

/**
 * @author dujunkai
 * @version 1.0
 * @description: run方法内没有sleep或wait方法时，停止线程
 * @date 2021/7/2 11:37
 */
public class RightWayStopThreadWithoutSleep {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("当前线程名：" + Thread.currentThread().getName());
        Runnable runnable = ()->{
            System.out.println("当前线程名：" + Thread.currentThread().getName());
            int num = 0;
            while(!Thread.currentThread().isInterrupted() && num <= Integer.MAX_VALUE / 2){
                if (num % 1000000 == 0){
                    System.out.println(num + "是10000的倍数");
                }
                num++;
            }
            System.out.println("任务结束");
        };
        Thread thread = new Thread(runnable);
        thread.start();
        System.out.println(Thread.currentThread().getName() + "线程休息0.5秒");
        Thread.sleep(500);
        thread.interrupt();
    }


}
